package admin

import (
	_ "embed"
	. "go-caipu/pkg/services/sqlstore/migrator"
)

//go:embed sql/sys_menu.sql
var menuSQLByte []byte

//go:embed sql/sys_menu_api.sql
var menuApiSQLByte []byte

func AddMenuMigrations(mg *Migrator) {
	userV1 := Table{
		Name: "sys_menu",
		Columns: []*Column{
			{Name: "menu_id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: false},
			{Name: "menu_name", Type: DB_Varchar, Length: 64, Nullable: false},
			{Name: "title", Type: DB_NVarchar, Length: 128, Nullable: false},
			{Name: "icon", Type: DB_NVarchar, Length: 128, Nullable: false},
			{Name: "path", Type: DB_NVarchar, Length: 128, Nullable: true},
			{Name: "paths", Type: DB_Varchar, Length: 300, Nullable: true},
			{Name: "menu_type", Type: DB_Int, Length: 4, Nullable: true},
			{Name: "status", Type: DB_Int, Length: 4, Nullable: true},
			{Name: "action", Type: DB_NVarchar, Length: 16, Nullable: true},
			{Name: "permission", Type: DB_NVarchar, Length: 255, Nullable: true},
			{Name: "parent_id", Type: DB_BigInt, Length: 11, Nullable: true},
			{Name: "no_cache", Type: DB_Bool, Length: 1, Nullable: true},
			{Name: "breadcrumb", Type: DB_NVarchar, Length: 255, Nullable: true},
			{Name: "component", Type: DB_NVarchar, Length: 255, Nullable: true},
			{Name: "redirect", Type: DB_NVarchar, Length: 255, Nullable: true},
			{Name: "sort", Type: DB_Int, Length: 4, Nullable: true},
			{Name: "visible", Type: DB_Int, Length: 1, Nullable: true},
			{Name: "is_frame", Type: DB_NVarchar, Length: 1, Nullable: true, Default: "0"},

			{Name: "create_by", Type: DB_BigInt, Nullable: false},
			{Name: "update_by", Type: DB_BigInt, Nullable: true},
			{Name: "created_at", Type: DB_DateTime, Nullable: false},
			{Name: "updated_at", Type: DB_DateTime, Nullable: true},
			{Name: "deleted_at", Type: DB_DateTime, Nullable: true},
		},
		Indices: []*Index{
			{Cols: []string{"menuName"}, Type: UniqueIndex},
		},
	}

	mg.AddMigration("create sys_menu table", NewAddTableMigration(userV1))

	menuApi := Table{
		Name: "sys_menu_api",
		Columns: []*Column{
			{Name: "sys_menu_menu_id", Type: DB_BigInt},
			{Name: "sys_api_id", Type: DB_BigInt},
		},
		Indices: []*Index{
			{Cols: []string{"sys_menu_menu_id", "sys_api_id"}, Type: UniqueIndex},
		},
	}
	mg.AddMigration("create table sys_menu_api", NewAddTableMigration(menuApi))
	//index
	mg.AddMigration("add index Ix_sys_menu_api", NewAddIndexMigration(menuApi, menuApi.Indices[0]))

	//add: menu data
	mg.AddMigration("insert into sys_menu", NewRawSQLMigration(string(menuSQLByte)))
	//add: menu api data
	mg.AddMigration("insert into sys_menu_api", NewRawSQLMigration(string(menuApiSQLByte)))
}
